package com.jiahua.utils.string;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class StringUtil {

	public static String md5(String s) {
		try {
			MessageDigest m = MessageDigest.getInstance("MD5");
			m.update(s.getBytes(), 0, s.length());
			BigInteger i = new BigInteger(1, m.digest());
			return String.format("%12x", i);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * null check
	 * 
	 * @param strField
	 *            文字列
	 * @return True(nullか空文字)/False(それ以外)
	 */
	public static boolean isNull(String strField) {
		if (strField == null || strField.equals("")) {
			return true;
		}
		return false;
	}

	/**
	 * @param strField
	 * @return boolean
	 */
	public static boolean isNotNull(String strField) {
		if (strField == null || strField.equals("")) {
			return false;
		}
		return true;
	}

	public static String genPassword(int length) {
		String alphabet = new String("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
		int n = alphabet.length();

		String result = new String();
		Random r = new Random();
		for (int i = 0; i < length; i++)
			result = result + alphabet.charAt(r.nextInt(n));
		return result;
	}

	public static String genRumdomString(int length) {
		String alphabet = new String("0123456789abcdefghijklmnopqrstuvwxyz");
		int n = alphabet.length();

		String result = new String();
		Random r = new Random();
		for (int i = 0; i < length; i++)
			result = result + alphabet.charAt(r.nextInt(n));
		return result;
	}

	public static String genRumdomNumber(int length) {
		String alphabet = new String("0123456789");
		int n = alphabet.length();

		String result = new String();
		Random r = new Random();
		for (int i = 0; i < length; i++)
			result = result + alphabet.charAt(r.nextInt(n));
		return result;
	}

	public static String genPhoneNum() {
		String alphabet01 = new String("789");
		int n = alphabet01.length();

		String result01 = new String();
		Random r = new Random();
		for (int i = 0; i < 1; i++)
			result01 = result01 + alphabet01.charAt(r.nextInt(n));

		String alphabet02 = new String("0123456789");
		int m = alphabet02.length();

		String result02 = new String();
		Random r2 = new Random();
		for (int i = 0; i < 8; i++)
			result02 = result02 + alphabet02.charAt(r2.nextInt(m));

		return "0" + result01 + "0" + result02;
	}

	public static String getDate() {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sf.format(new Date());
		return date;
	}

	public static boolean hasXssChars(String str) {
		if (str.matches(".*[;:<>&\"'].*")) {
			return true;
		} else {
			return false;
		}
	}

	public static String haldlerNull(Object str) {
		if (str == null || "null".equals(str)) {
			return "";
		}

		System.out.println("str : " + String.valueOf(str));
		return String.valueOf(str);
	}

	public static boolean isUrl(String url) {
		if (url.matches("http://.*") || url.matches("https://.*")) {
			return true;
		} else {
			return false;
		}
	}

	public static String getBirthday() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
		Date date = new Date();
		String now = sdf.format(date);
		return now;
	}

	public static String getRegDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
		Date date = new Date();
		String now = sdf.format(date);
		return now;
	}

	public static String getLastestLoginDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
		Date date = new Date();
		String now = sdf.format(date);
		return now;
	}

	public static String getDateFromMilliseconds(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS", Locale.US);
		Date date = new Date(time);
		String now = sdf.format(date);
		return now;
	}

	public static String genKatanaka(int length) {
		String alphabet = new String("見現在時刻ミリ秒空メモリ量違順序乱数発生ジェネレータ生成");
		int n = alphabet.length();

		String result = new String();
		Random r = new Random();
		for (int i = 0; i < length; i++)
			result = result + alphabet.charAt(r.nextInt(n));
		return result;
	}

	public static String regularExpressionsOfHTML(String tempContent) {
		String result = "";
		//String regex = "<[^>]+>";
		result = tempContent.replaceAll("<[^>]+>", ""); // 任意多个
		return result;
	}

	public static void main(String args[]) {

		// String test01 = "会社'名";
		// String test02 = "氏名;";
		// String test03 = "氏名:";
		//
		// if (hasXssChars(test01)) {
		//
		// System.out.println(" test01 : " + replaceSingleQuator(test01) +
		// " NG");
		// } else {
		// System.out.println(" test01 : " + test01 + " OK");
		// }
		//
		// if (hasXssChars(test02)) {
		//
		// System.out.println(" test02 NG");
		// } else {
		// System.out.println(" test02 OK");
		// }
		//
		// if (hasXssChars(test03)) {
		//
		// System.out.println(" test03 NG");
		// } else {
		// System.out.println(" test03 OK");
		// }

		// System.out.println(genPassword(6));
		// System.out.println(getDate());

		System.out.println(genPhoneNum());

	}

	public static String replaceSingleQuator(String str) {

		return haldlerNull(str).replaceAll("'", "\\'");
	}

}
